﻿using System;
using System.Text;
using System.Net;


namespace ROUDP
{
	public class GameServer
	{
		private MyUDPAgent udp;
		private KCP kcp;
		public GameServer()
		{
			udp = new MyUDPAgent(6060);
			kcp = new KCP();
			kcp.outputFunc = udp.Send;
			udp.targetPoint = new IPEndPoint(IPAddress.Loopback, 5050);

		}
		//send  recv
		//main thread
		public void Run()
		{
			var start = DateTime.UtcNow;
			var delta = (DateTime.UtcNow - start).TotalMilliseconds;
			var lastTime = start;

			double accTime = 0.0f;
			int c = 0;
			while (true)
			{
				var data = udp.RecvData();
				if (data != null)
				{
					kcp.Input(data, data.Length);
				}
				var now = DateTime.UtcNow;
				delta = (now - lastTime).TotalSeconds;
				lastTime = now;

				kcp.Update(delta);


				accTime += delta;
				if (accTime > 1.0)
				{
					accTime -= 1.0;
					//var buff = Encoding.ASCII.GetBytes("helloWorld");
					//kcp.Send(buff);
				}
				var rdata = kcp.Recv();
				if (rdata != null)
				{
					var st = Encoding.ASCII.GetString(rdata);
					Console.WriteLine("ClientRecv:" + st + ":" + c++);
				}
			}
		}
	}

	class MainClass
	{
		public static void Main(string[] args)
		{
			Console.WriteLine("Hello World!");
			var gs = new GameServer();
			gs.Run();
		}
	}
}
